import sim, sim_items, main
from util import *
import random

class VampiricPrefix(sim_items.Prefix):
    """
    "vampiric" prefix causes affected weapon to heal its wielder by amount
    proportional to damage dealt each hit. But it also damages the wielder each
    time it is wielded.
    """
    def __init__(self):
        super(VampiricPrefix, self).__init__(
            "vampiric",
            "This %(wearable)s could suck a golf ball through a garden "
            "hose!"
        )

    def initialize(self, wearable):
        if not isinstance(wearable, sim_items.Weapon):
            raise ValueError('Tried to apply %r to non-weapon %r.' % (self,
                wearable))
        super(VampiricPrefix, self).initialize(wearable)
        main.schedule.listen('wear', self)
        main.schedule.listen('decrease stat', self)

    def remove(self, wearable):
        super(VampiricPrefix, self).remove(wearable)
        main.schedule.unlisten('wear', self)
        main.schedule.unlisten('decrease stat', self)

    def handle_wear(self, event):
        """
        wear             (item, creature, slot)
        """
        wearable, creature, slot = event.args
        if wearable is not self.wearable:
            return

        # deal damage to the creature wielding us
        dmg = (.15 + .10*random.random()) * creature.maxHP
        if creature is main.player:
            main.transient('You shudder in icy pain as you wield %s!'\
                    % wearable.noun(definite=True, annotation=False))
        elif visible(creature):
            main.transient('%s shudders in pain as %s wields %s!'\
                    % (creature.noun(definite=True, capitalized=True),
                       creature.pronounNom(), wearable.noun(definite=True),),
                    creature.parentTile)
        creature.lastDamageSource = wearable
        main.schedule.sendNow(sim.Event('decrease stat', main.schedule.time,
                (creature, 'hp', dmg), dests=[creature]))


    def handle_decrease_stat(self, event):
        """
        decrease stat    (creature, statname, amount)
        """
        parole.debug('vampiric.handle_decrease_stat...')
        creature, statname, amount = event.args
        wielder = self.wearable.wearer
        if wielder is None or wielder is creature:
            # so for example, throwing a vampiric weapon won't heal you when it
            # strikes
            # also, don't heal the wielder in response to the damage from
            # wielding us (that blew my mind)
            return

        parole.debug('vampiric.handle_decrease_stat: last dmg src: %r',
                creature.lastDamageSource)

        if (statname != 'hp') or (creature.lastDamageSource is not wielder) \
                or (amount <= 0):
            return

        parole.debug('vampiric.handle_decrease_stat: our wearable did damage')
        # if we didn't return, that means our weapon (wearable) just hurt somebody

        # no message (would be spammy)
        healAmount = amount / 4.0
        parole.debug('vampiric.handle_decrease_stat: healing by %s', healAmount)
        main.schedule.sendNow(sim.Event('increase stat', main.schedule.time,
                (wielder, 'hp', healAmount), dests=[wielder]))

#========================================
thingClass = VampiricPrefix
